/**
 *
 * Module        : Module Size
 * Authors       : Ben Kwint & Arie van der Veek
 * Date          : November 13th 2011
 * Description   : A few functions to do some string operations
 *
 */
module Metrics::Regular::StringUtils

import IO;
import lang::java::jdt::Java;
import Number;
import String;



// example  splitStringInLines("\nbladiebla \n bladieblad \n  bla");
// example  println("\nbladiebla \n bladieblad \n  bla");
// Got issue with it,leave till tomorrow.
public list[str] splitStringInLines(str line){
		list [str] lines =  [];
		 while (/(?s)<before:[^\n]*>\n<after:.*>/ := line) {
   				lines += [before];
    			line = after;
  		}
  		lines += [line];
  		return lines;
}

public str getQualifiedMethodsName(Entity entity){
      str name = "";
      top-down-break visit(entity)
      {
        case package(a): name = name + a + ".";
        case class(a): name = name + a + ".";
        case method(a,_,_): name = name + a + "()";
      };
      return name;
 }
 
 public str getQualifiedClassName(Entity entity){
      str name = "";
      top-down-break visit(entity)
      {
        case package(a): name = name + a + ".";
        case class(a): name = name + a;
      };
      return name;
 }
 
  public str getPackageName(Entity entity){
      str name = "";
      top-down-break visit(entity)
      {
        case package(a): name = name + a + ".";
      };
      name = replaceLast(name, ".", "");
      return name;
 }
 
 // Function to truncate a Real. Not as nice as real rounding, but works
// for this purpose. Heavily inspired by Jurgen Vinju's impl from
// http://tutor.rascal-mpl.org/Courses/Rascal/Rascal.html#/Courses/Rascal/Expressions/Values/Real/Real.html
public str truncateRealToString(real number, int decimalPoints) {
	real tmpReal = number;
	if (/<prefix:\d+\.\d{<decimalPoints>}>/ := "<number>") tmpReal = toReal(prefix);
	return toString(tmpReal);
}